package com.examination.utils;


import com.examination.bean.Selection;
import com.examination.controller.login.LoginServlet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.annotation.Annotation;
import java.lang.reflect.*;
import java.sql.*;
import java.util.*;
import java.util.Date;

public class Test {
    public static void main(String[] args) {

        System.out.println(((char)(65+1))+"");

        /*Test ls = new Test();
        System.out.println("hello");
        // 执行方法
       try {
            Method method = null;
            try {
                method = ls.getClass().getDeclaredMethod("login", String.class, String.class);
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }
            method.invoke(ls, null, null);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }*/
    }
    public void login(String s,String s1){
        System.out.println("hello login");
    }
    private static void test3(Object obj) throws Exception {
//        Class clazz = obj.getClass();
//        ormSet(obj);
        List<Selection> selectionList = null;
        List<Map<String, Object>> objMapList = null;
        //数据库查询得到的
        Connection conn = DbUtil.getConnInstance();
        //3.操作数据库，实现增删改查
        ResultSet rs = null;
        PreparedStatement stmt = null;
        try {
            String sql = "SELECT * FROM e_selection where type = ?";
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, "single_choice");
            rs = stmt.executeQuery();
            selectionList = DbUtil.getObjectList(Selection.class,rs);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if(null != selectionList){
            for (int i = 0; i < selectionList.size(); i++) {
                System.out.println(selectionList.get(i).toString());
            }
        }
    }

    /**
     *通过数据库结果集返回对象集合
     * @param clazz 对象类型
     * @param rs 数据库结果集
     * @return List<Object>封装的对象类型
     */
    public static <T> List<T> getObjectList(Class<T> clazz,ResultSet rs) throws Exception {
        List<T> objectList = new ArrayList<T>();
        if (null != rs) {
            //表原数据
            ResultSetMetaData rsmd = rs.getMetaData();
            String tableName = rs.getMetaData().getTableName(1);//就可以返回表名
            T objTemp = null;
            Map mapObj = null;
            int numberOfColumns = rsmd.getColumnCount(); //得到数据集的列数
            //判断是否存在记录
            if (rs.next()) {
                //存在记录 rs就要向上移一条记录 因为rs.next会滚动一条记录了
                rs.previous();
                while (rs.next()) {
                    objTemp = clazz.newInstance();
                    //准备将1行数据封装到JavaBean
                    String columnName;//数据库字段名
                    Object columnVal = null;//字段列值
                    String columnTypeName;//字段数据类型
                    for (int i = 1; i <= numberOfColumns; i++) {
                        columnName = rs.getMetaData().getColumnName(i);
                        columnTypeName = rs.getMetaData().getColumnTypeName(i);
                        columnVal = rs.getObject(columnName);
                        ReflectionUtil.setObjectValue(objTemp, columnName, columnVal);
                    }
                    objectList.add(objTemp);
                }
            }
        }
        return objectList;
    }

    /**
     * 获得属性值
     *
     * @param object
     * @throws Exception
     */
    public static void getObjectValue(Object object) throws Exception {
        // 拿到该类
        Class<?> clz = object.getClass();
        // 获取实体类的所有属性，返回Field数组
        Field[] fields = clz.getDeclaredFields();
        for (Field field : fields) {// --for() begin
            System.out.println(field.getGenericType());//打印该类的所有属性类型
            // 如果类型是String
            if (field.getGenericType().toString().equals(
                    "class java.lang.String")) { // 如果type是类类型，则前面包含"class "，后面跟类名
                // 拿到该属性的gettet方法
                /**
                 * 这里需要说明一下：他是根据拼凑的字符来找你写的getter方法的
                 * 在Boolean值的时候是isXXX（默认使用ide生成getter的都是isXXX）
                 * 如果出现NoSuchMethod异常 就说明它找不到那个gettet方法 需要做个规范
                 */
                Method m = (Method) object.getClass().getMethod(
                        "get" + getMethodName(field.getName()));
                String val = (String) m.invoke(object);// 调用getter方法获取属性值
                if (val != null) {
                    System.out.println("String type:" + val);
                }
            }
            // 如果类型是Integer
            if (field.getGenericType().toString().equals(
                    "class java.lang.Integer")) {
                Method m = (Method) object.getClass().getMethod(
                        "get" + getMethodName(field.getName()));
                Integer val = (Integer) m.invoke(object);
                if (val != null) {
                    System.out.println("Integer type:" + val);
                }
            }
            // 如果类型是Long
            if (field.getGenericType().toString().equals(
                    "class java.lang.Long")) {
                Method m = (Method) object.getClass().getMethod(
                        "get" + getMethodName(field.getName()));
                Long val = (Long) m.invoke(object);
                if (val != null) {
                    System.out.println("Long type:" + val);
                }
            }
            // 如果类型是Float
            if (field.getGenericType().toString().equals(
                    "class java.lang.Float")) {
                Method m = (Method) object.getClass().getMethod(
                        "get" + getMethodName(field.getName()));
                Float val = (Float) m.invoke(object);
                if (val != null) {
                    System.out.println("Float  type:" + val);
                }
            }
            // 如果类型是Double
            if (field.getGenericType().toString().equals(
                    "class java.lang.Double")) {
                Method m = (Method) object.getClass().getMethod(
                        "get" + getMethodName(field.getName()));
                Double val = (Double) m.invoke(object);
                if (val != null) {
                    System.out.println("Double type:" + val);
                }
            }
            // 如果类型是Boolean 是封装类
            if (field.getGenericType().toString().equals(
                    "class java.lang.Boolean")) {
                Method m = (Method) object.getClass().getMethod(
                        field.getName());
                Boolean val = (Boolean) m.invoke(object);
                if (val != null) {
                    System.out.println("Boolean type:" + val);
                }
            }
            // 如果类型是Date
            if (field.getGenericType().toString().equals(
                    "class java.util.Date")) {
                Method m = (Method) object.getClass().getMethod(
                        "get" + getMethodName(field.getName()));
                Date val = (Date) m.invoke(object);
                if (val != null) {
                    System.out.println("Date type:" + val);
                }
            }
            // 如果类型是Short
            if (field.getGenericType().toString().equals(
                    "class java.lang.Short")) {
                Method m = (Method) object.getClass().getMethod(
                        "get" + getMethodName(field.getName()));
                Short val = (Short) m.invoke(object);
                if (val != null) {
                    System.out.println("Short type:" + val);
                }
            }
            // 如果类型是Character
            if (field.getGenericType().toString().equals(
                    "class java.lang.Character")) {
                Method m = (Method) object.getClass().getMethod(
                        "get" + getMethodName(field.getName()));
                Character val = (Character) m.invoke(object);
                if (val != null) {
                    System.out.println("Character type:" + val);
                }
            }
        }//for() --end
    }
    // 把一个字符串的第一个字母大写、效率是最高的、 a-A b-B x-X
    private static String getMethodName(String fildeName) throws Exception {
        byte[] items = fildeName.getBytes();
        items[0] = (byte) ((char) items[0] - 'a' + 'A');
        return new String(items);
    }

    /**
     * 获得所有属性信息
     * @param obj
     * @throws Exception
     */
    private static void testFields(Object obj) throws Exception {
        Class clazz = obj.getClass();
        Field[] fileds=clazz.getDeclaredFields();
        for (Field field : fileds) {
//获取访问修饰符
            int i=field.getModifiers();
            String m=Modifier.toString(i);
            System.out.println("访问修饰符 ： "+i+" :::: "+m);
//获取属性的类型
            System.out.println("数据类型 ： "+field.getType());
//获取变量名称
            System.out.println("变量名称 ： "+field.getName());
        }

    }

    /**
     * 获取方法的相关信
     *
     * @throws Exception
     */
    private static void test2(Object obj) throws Exception {
        Class clazz = obj.getClass();
        Method[] method = clazz.getDeclaredMethods();
        for (Method method2 : method) {
            System.out.println("注解：");
            Annotation[] ann = method2.getAnnotations();
            for (Annotation annotation : ann) {
                System.out.print(annotation + "\t  --");
            }
            System.out.println();
            System.out.println("访问修饰符：" + method2.getModifiers() + ":" + Modifier.toString(method2.getModifiers()));
            System.out.println("方法名称:" + method2.getName());
            System.out.println("返回值类型：" + method2.getReturnType());
            System.out.print("形参列表：");
            Class[] types = method2.getParameterTypes();
            for (Class class1 : types) {
                System.out.print(class1.getName() + "\t ");
            }
            System.out.println();
            System.out.print("异常类型：");
            Class[] exceptions = method2.getExceptionTypes();
            for (Class class1 : exceptions) {
                System.out.print(class1 + "\t");
            }
            System.out.println();
        }
    }

    /**
     * 获取每个属性的访问修饰符    属性类型    属性名称
     */
    private static void test1() {
        Class clazz = Selection.class;
        Field[] fileds = clazz.getDeclaredFields();
        for (Field field : fileds) {
//获取访问修饰符
            int i = field.getModifiers();
            String m = Modifier.toString(i);
            System.out.println("#####" + field.getGenericType());
            System.out.println("访问修饰符 ： " + i + " :::: " + m);
//获取属性的类型
            System.out.println("数据类型 ： " + field.getType());
//获取变量名称
            System.out.println("变量名称 ： " + field.getName());
        }

    }

    public static void test0() throws IllegalAccessException, InstantiationException, NoSuchFieldException, NoSuchMethodException, InvocationTargetException {
      /*  Selection sel = new Selection();
        sel.setType("aaa");
        System.out.println(sel.getType());*/

        Class clazz = Selection.class;
//创建clazz对应的运行时类的Person类的对象
        Selection sel = (Selection) clazz.newInstance();
        String info = sel.toString();

        sel.setType("aaa");
        System.out.println(sel.getType());
//通过反射运行时类，加载类的属性
        Field f1 = clazz.getField("type");
        f1.set(sel, "#############");
        System.out.println(sel.getType());
////通过反射运行时类加载类的方法

        Method method1 = clazz.getMethod("getType");
        Class<?> returnType = method1.getReturnType();
        System.out.println("####" + returnType.getTypeName());
        Class<?>[] paramTypes = method1.getParameterTypes();
        for (int i = 0; i < paramTypes.length; i++) {
            System.out.println("paramert_####" + paramTypes[i].getTypeName());
        }
        System.out.println(method1.invoke(sel));
////方法的参数类型是Calss类型，为了确保类型的确定性，所以String.class，多个参数就用，逗号隔开
        Method method2 = clazz.getMethod("setType", String.class);
        Class<?> method2returnType = method2.getReturnType();
        System.out.println("method2returnType####" + method2returnType.getTypeName());
        Class<?>[] method2returnTypeparamTypes = method2.getParameterTypes();
        for (int i = 0; i < method2returnTypeparamTypes.length; i++) {
            System.out.println("method2returnTypeparamTypesparamert_####" + method2returnTypeparamTypes[i].getTypeName());
        }

        method2.invoke(sel, "CHINA");
        System.out.println(sel.getType());

    }


}

